package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.Editable;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class Login extends AppCompatActivity {
    Button Login;
    Button Logup;
    EditText UserName;
    EditText PassWord;
    TextView tip;
    public static final String sqlUserName="root";
    public static final String sqlPassWord="tzyTZY123";

    public static final String Url="jdbc:mysql://cdb-l1c6g0ne.bj.tencentcdb.com:10166/AndroidUser?user=root&password=tzyTZY123";
    public static final String Driver="com.mysql.jdbc.Driver";
    private static Connection connection = null;
    private static PreparedStatement preparedStatement = null;
    private static ResultSet resultSet;
    public  boolean LoginVerify(String inputUserName,String inputPassWord) throws ClassNotFoundException {//登录成功为true，反之为false
        ArrayList<String> password = new ArrayList<>();
        try {
            //1,加载驱动
            Class.forName(Driver);
            //2.创建连接
            connection = DriverManager.getConnection(Url);
            String MySql = "select * from android_user where UserName=?";//?代表参数会在以后加
//            String MySql = "select * from android_user";
            preparedStatement = connection.prepareStatement(MySql);
            preparedStatement.setString(1, inputUserName);//代表第一个？放置这个参数
            resultSet =  preparedStatement.executeQuery();
            //遍历查询结果集
            password=new ArrayList<>();
            while(resultSet.next()){
                password.add(resultSet.getString("PassWord"));
            }
        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
        }
        System.out.println(password);
        if(password.size()==0){
            return false;
        }
        else if (password.get(0).equals(inputPassWord)){
            return true;
        }
        else{
            return false;
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        tip=(TextView)findViewById(R.id.tip) ;
        Login=(Button)findViewById(R.id.Login);
        Logup=(Button)findViewById(R.id.Logup);
        UserName=(EditText)findViewById(R.id.UserName);
        PassWord=(EditText)findViewById(R.id.PassWord);
    }

    public void ClickLogin(View view) throws ClassNotFoundException {//登录
        System.out.println(UserName.getText());
        System.out.println(PassWord.getText());
        Editable theUserName=UserName.getText();
        Editable thePassWord=PassWord.getText();
        new Thread(new Runnable(){
            @Override
            public void run() {
                try {
                    if(LoginVerify(theUserName.toString(),thePassWord.toString())){
                        startActivity(new Intent(Login.this,SelectItem.class));
                    }
                    else {
                        tip.setText("PassWord Error");

                    }
                    System.out.println(LoginVerify(theUserName.toString(),thePassWord.toString()));
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }
    public void ClickLogup(View view){

    }

}